<?php


namespace app\system\service;

class RoleService
{
	public static function getlist($options)
	{
		$result = \think\Db::name("system_admin_role_auth r")->join("system_admin_menu m", "r.menu_auth_id=m.id")->order(["sort" => "desc"])->field(["m.name" => "name", "r.role_id" => "role_id"])->select();
		$role_id_menu_auth_names = [];
		foreach ($result as $k => $v) {
			if (isset($role_id_menu_auth_names[$v["role_id"]])) {
				$role_id_menu_auth_names[$v["role_id"]] .= $v["name"] . ",";
			} else {
				$role_id_menu_auth_names[$v["role_id"]] = $v["name"] . ",";
			}
		}
		$lists = \think\Db::name("system_admin_role")->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("system_admin_role")->count();
		$pages = ceil($count / $options["limit"]);
		foreach ($lists as $k => $v) {
			$lists[$k]["auth_str"] = isset($role_id_menu_auth_names[$v["id"]]) ? $role_id_menu_auth_names[$v["id"]] : "";
			$lists[$k]["auth_str"] = rtrim($lists[$k]["auth_str"], ",");
			$lists[$k]["auth_ids"] = \think\Db::name("system_admin_role_auth")->where(["role_id" => $lists[$k]["id"]])->column("menu_auth_id");
			$lists[$k]["create_time_str"] = date("Y-m-d H:i:s", $v["create_time"]);
		}
		return ["code" => \app\system\model\Code::success, "data" => $lists, "count" => $count, "pages" => $pages];
	}
	public static function add($options)
	{
		\think\Db::startTrans();
		try {
			$data = ["name" => $options["name"], "desc" => $options["desc"], "create_time" => time()];
			$role_id = \think\Db::name("system_admin_role")->insertGetId($data);
			$data = [];
			$options["auth_ids"] = empty($options["auth_ids"]) ? [] : $options["auth_ids"];
			foreach ($options["auth_ids"] as $k => $v) {
				$data[] = ["role_id" => $role_id, "menu_auth_id" => $v];
			}
			\think\Db::name("system_admin_role_auth")->insertAll($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function getinfo($options)
	{
		$data = \think\Db::name("system_admin_role")->where(["id" => $options["id"]])->field(["id", "name", "desc"])->find();
		if (empty($data)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "ID不存在"];
		}
		$data["auth_ids"] = \think\Db::name("system_admin_role_auth")->where(["role_id" => $options["id"]])->column("menu_auth_id");
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function edit($options)
	{
		\think\Db::startTrans();
		try {
			$data = ["name" => $options["name"], "desc" => $options["desc"], "update_time" => time()];
			if (!empty($options["desc"])) {
				$data["desc"] = $options["desc"];
			}
			\think\Db::name("system_admin_role")->where(["id" => $options["id"]])->update($data);
			$data = [];
			\think\Db::name("system_admin_role_auth")->where(["role_id" => $options["id"]])->delete();
			$options["auth_ids"] = empty($options["auth_ids"]) ? [] : $options["auth_ids"];
			foreach ($options["auth_ids"] as $k => $v) {
				$data[] = ["role_id" => $options["id"], "menu_auth_id" => $v];
			}
			\think\Db::name("system_admin_role_auth")->insertAll($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$role = \think\Db::name("system_admin_role")->where(["id" => $options["id"]])->find();
			if (empty($role)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "不存在ID"];
			}
			if (\think\Db::name("system_admin")->where(["role_id" => $options["id"]])->count() > 0) {
				return ["code" => \app\system\model\Code::fail, "msg" => "该角色下存在管理员，无法删除"];
			}
			\think\Db::name("system_admin_role")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}